package com.jinyi.wwdeng.ui.user.fragment

import android.annotation.SuppressLint
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.jinyi.base.network.IRequestCallback
import com.jinyi.base.network.NetworkUtils
import com.jinyi.base.utils.BarUtils
import com.jinyi.base.utils.SPUtils
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.FragmentMenuBinding
import com.jinyi.wwdeng.network.ApiService
import com.jinyi.wwdeng.network.HttpBaseParams.addCommonParams
import com.jinyi.wwdeng.pro.ProFragment
import com.jinyi.wwdeng.ui.login.LoginAct
import com.jinyi.wwdeng.ui.user.act.UserAccountAct
import com.jinyi.wwdeng.ui.user.act.UserDataPrivacyAct
import com.jinyi.wwdeng.ui.user.act.UserFeedbackAct
import com.jinyi.wwdeng.utils.dialog.StarDialogContract
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils
import org.json.JSONObject


/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/17 14:34
 * @Annotation：个人中心
 */
class UserFragment(drawer_layout: DrawerLayout): ProFragment() {

    lateinit var binding: FragmentMenuBinding

    private var drawerLayout = drawer_layout

    override fun getRootView(inflater: LayoutInflater): View {
        binding = FragmentMenuBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        rootViewBinding.containerLayout.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.transparent))
        binding.contentLayout.setPadding(0,BarUtils.getStatusBarHeight(),0,0)
    }

    override fun initListener() {
        //关闭
        binding.closeLayout.setOnClickListener {
            drawerLayout.closeDrawer(GravityCompat.START)
        }

        //我的账户
        binding.ivAvatar.setOnClickListener {
//            binding.closeLayout.callOnClick()
//            Handler().postDelayed({
//                onIntent(UserAccountAct::class.java)
//            },300)
        }

        //意见反馈
        binding.feedbackLayout.setOnClickListener {
            binding.closeLayout.callOnClick()
            Handler().postDelayed({
                onIntent(UserFeedbackAct::class.java)
            },300)
        }

        //数据安全
        binding.dataPrivacyLayout.setOnClickListener {
            onIntent(UserDataPrivacyAct::class.java)
        }

        //我的账户、钱包
        binding.accountLayout.setOnClickListener {
            binding.closeLayout.callOnClick()
            Handler().postDelayed({
                onIntent(UserAccountAct::class.java)
            },300)
        }

        //打分
        binding.rateUsLayout.setOnClickListener {
            onRateUsAction()
        }

        //退出登录
        binding.loginOutLayout.setOnClickListener {
            binding.closeLayout.callOnClick()
            TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_USER_LOGINOUT,"")
            //移除已登录状态
            SPUtils.remove(AppConfigs.SP_KEY.SP_HENECREDIT_LOGIN)
            //移除用户Token
            SPUtils.remove(AppConfigs.SP_KEY.SP_HENECREDIT_TOKEN)
            //移除用户id
            SPUtils.remove(AppConfigs.SP_KEY.SP_HENECREDIT_USERID)
            Handler().postDelayed({
                onIntent(LoginAct::class.java)
            },300)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        //手机号码
        val mobile = AppConfigs.getSPString(AppConfigs.SP_KEY.SP_HENECREDIT_MOBILE)
        binding.tvMobile.text = "+$mobile"

        //获取版本名称
        val packageInfo = requireContext().packageManager.getPackageInfo(requireContext().packageName, 0)
        val versionCode = packageInfo.versionCode // 版本号（整数）
        binding.tvVersion.text = String.format(getString(R.string.version), packageInfo.versionName)

    }

    /**
     * 打分
     */
    private fun onRateUsAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.API_PATH_SCORE_COMMENT)
        }
        NetworkUtils.post(httpUrl,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")){
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null) {
                            StarDialogContract.showScoreDialog(requireContext(),obj,false)
                        }
                    }
                    else -> {//失败
                        showToast(data.optString("msg"))
                    }
                }
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                showToast(data.optString("errorMsg"))
            }
            //请求完成
            override fun onRequestFinish() {
            }
        })
    }

    override fun onStart() {
        super.onStart()
        TrackEventUtils.logEvent(TrackEventType.PAGE.PAGE_USER_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        TrackEventUtils.logEvent(TrackEventType.PAGE.PAGE_USER_END,"")
    }
}